package com.ihighest.app.infox.activity;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.core.Application;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.OnlineVoteAdapter;
import com.ihighest.app.infox.myview.ActivityTitle;

/**
 * QuestionnaireActivity
 * 
 * 调查问卷界面
 * 
 * @author yyh Created at 2015-10-17 下午2:30:08
 * @version 1.0
 */
public class QuestionnaireActivity extends BaseActivity implements OnCheckedChangeListener {
	private ActivityTitle title;

	RadioButton inProcess, takePartIn, finish;
	private String url;
	private PullToRefreshListView voteListView;
	private int mPage = 1;
	private String type = "1";// 加载数据类型默认时进行中
	private long mTime;
	private OnlineVoteAdapter adapter;
	private JSONArray array;
	private JSONArray moreArray;
	private TextView nodate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Application.getInstance().addActivity(this);
		setContentView(R.layout.infox_activity_questionnaires);

		title = (ActivityTitle) findViewById(R.id.id_questionnaires_title);

		title.setTitle("调查问卷");
		title.setViewVersible(title.titleBack);
		title.setViewGone(title.titleRefresh, title.titleMsg, title.titleOther, title.titleSearch, title.rightLayout);

		inint();

	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getData(mPage, 0, type);
	}

	private void inint() {

		inProcess = (RadioButton) findViewById(R.id.infox_question_inProgress);
		takePartIn = (RadioButton) findViewById(R.id.infox_question_takePartIn);
		finish = (RadioButton) findViewById(R.id.infox_question_finish);
		voteListView = (PullToRefreshListView) findViewById(R.id.questionListview);
		nodate = (TextView) findViewById(R.id.noQuestionData);
		inProcess.setChecked(true);// 默认选择正在进行
		inProcess.setOnCheckedChangeListener(this);
		takePartIn.setOnCheckedChangeListener(this);
		finish.setOnCheckedChangeListener(this);
		adapter = new OnlineVoteAdapter(QuestionnaireActivity.this, array);
		voteListView.setAdapter(adapter);
		//getData(mPage, 0, type);
		voteListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(QuestionnaireActivity.this, QuestionSurveyDetailActivity.class);
				try {
					intent.putExtra("id", array.getJSONObject(position - 1).getString("id"));
					intent.putExtra("isJoin", array.getJSONObject(position - 1).getString("join"));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				startActivity(intent);
			}
		});

	}

	@Override
	public void onCheckedChanged(CompoundButton button, boolean isCheck) {
		mPage = 1;
		if (isCheck) {
			switch (button.getId()) {
			case R.id.infox_question_inProgress:
				type = "1";
				array = new JSONArray();
				adapter = new OnlineVoteAdapter(QuestionnaireActivity.this, array);
				voteListView.setAdapter(adapter);
				getData(mPage, 0, type);
				break;

			case R.id.infox_question_takePartIn:
				type = "2";
				array = new JSONArray();
				adapter = new OnlineVoteAdapter(QuestionnaireActivity.this, array);
				voteListView.setAdapter(adapter);
				getData(mPage, 0, type);
				break;
			case R.id.infox_question_finish:
				array = new JSONArray();
				adapter = new OnlineVoteAdapter(QuestionnaireActivity.this, array);
				voteListView.setAdapter(adapter);
				type = "3";
				getData(mPage, 0, type);
				break;
			}

		}
	}

	private void getData(final int mPage2, int time, String mType) {
		ApiParam param = ApiParam.create();
		param.addParam("page", mPage2);
		if (mPage2 >= 2) {
			param.addParam("time", time);
		}

		param.addParam("type", type);
		Api.get("survey/list", param, new ApiCallback(QuestionnaireActivity.this,true) {

			@Override
			public void handleSuccess(ApiResult result) {
				if (mPage2 == 1) {
					if (result.getDatas() == null || result.getDatas().length() == 0) {
						nodate.setVisibility(View.VISIBLE);
						voteListView.setVisibility(View.GONE);
					} else {
						nodate.setVisibility(View.GONE);
						voteListView.setVisibility(View.VISIBLE);
					}

				}
				if (result.getDatas() == null || result.getDatas().length() == 0) {
					mPage = mPage2 > 1 ? --mPage : 1;
				} else {
					if (mPage2 == 1) {
						array = result.getDatas();
						adapter.setData(result.getDatas());
					} else {

						moreArray = result.getDatas();
						try {
							array = AppKit.joinJSONArray(array, moreArray);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						adapter.appendData(moreArray);
					}
					mTime = result.getTime();
				}

				voteListView.onRefreshComplete();
			}

			@Override
			public void handleFailure(Exception e, String msg) {
				// TODO Auto-generated method stub

			}
		}, QuestionnaireActivity.this);
	}
}